package cn.doways.api.auth;

import cn.doways.api.entity.Role;
import cn.doways.api.entity.Secret;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName JwtUserFactory
 * @Description //TODO
 * @Author shellming
 * @Date 2019-08-14 05:58
 **/
public class JwtUserFactory {
    private JwtUserFactory() {
    }
    public static JwtUsers create(Secret secret){
        return new JwtUsers(
                secret.getPhoneNumber(),
                secret.getId(),
                secret.getActiveStatus(),
                secret.getPasswordHash(),
                secret.getLastPasswordResetDate(),
                mapToGrantedAuthorities(secret.getRoles())
                );
    }
    private static List<GrantedAuthority> mapToGrantedAuthorities(List<Role> authorities) {
        List<GrantedAuthority> list=new ArrayList<>();
        for (Role role:authorities){
            list.add(new SimpleGrantedAuthority(role.getName()));
        }
        return list;
    }
}
